package de.mhus.lib.liferay;

import java.util.List;

import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.Role;
import com.liferay.portal.model.User;

public class MLiferayUtil {

	/**
	 * Returns true if the use is member of the given role name.
	 * 
	 * @param user user or null. If null it will return false
	 * @param role Role name or null. If null it will return false'
	 * @return
	 * @throws SystemException
	 */
	public static boolean hasRole(User user, String role) throws SystemException {
		if (user == null || role == null) return false;
		for (Role r : user.getRoles()) {
			if (r.getName().equals(role))
				return true;
		}
		return false;
	}

	/**
	 * Returns true if the user is member in one of this roles.
	 * 
	 * @param user
	 * @param roles
	 * @return
	 * @throws SystemException
	 */
	public static boolean hasRole(User user, List<String> roles) throws SystemException {
		if (user == null || roles == null) return false;
		for (Role r : user.getRoles()) {
			for (String role : roles) {
				if (role != null && r.getName().equals(role))
					return true;
			}
		}
		return false;
	}
	
}
